﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using Tauren.Form.Infrastructure;
using Tauren.Form.Infrastructure.Common;
using Tauren.Form.Service.Form_TableInfoBLL;

namespace Tauren.Form.Core.Controllers
{
    /// <summary>
    /// 表单表版本信息接口-v1.0
    /// </summary>
    [Route("api/formtable/v{version:apiVersion}")]
    [ApiController]
    public class Form_TableInfoController : ControllerBase
    {
        private IForm_TableInfoService service; 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="services"></param>
        public Form_TableInfoController(IEnumerable<IForm_TableInfoService> services)
        {
            this.service = services.FirstOrDefault(o => o.VersionName == 1.0);
            if (this.service == null) throw new ApiVersionException("不支持当前版本");
        }
        /// <summary>
        /// 分页获取 单位信息列表
        /// </summary>
        /// <param name="queryModel"></param>
        /// <returns></returns>
        [HttpPost, Route("pages")]
        [ApiVersion("1.0")] 
        public IActionResult Pages([FromBody] Entity.Global.QueryModel queryModel, [FromRoute] string version)
        {
            return service.Pages(queryModel).ToJsonResult();
        }

        /// <summary>
        /// 分页获取 单位信息列表
        /// </summary>
        /// <param name="queryModel"></param>
        /// <returns></returns>
        [HttpPost, Route("childs")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Childs([FromBody] Entity.Global.QueryModel queryModel) => service.Childs(queryModel).ToJsonResult();

        /// <summary>
        /// 获取字段数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("config/{type}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Config([FromRoute] string type) => service.Config(type).ToJsonResult(true);

        /// <summary>
        /// 获取单个表信息数据
        /// </summary>
        /// <param name="id">人员基本信息实体id</param>
        /// <returns></returns>
        [HttpPost, Route("get/{id}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Get([FromRoute] long id) => service.FirstOrDefault(id).ToJsonResult();
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Route("add")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Add([FromBody] Entity.Input.Form_TableInfo_InPut input) => service.Add(input).ToJsonResult();


        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Route("edit")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Editor([FromBody] Entity.Input.Form_TableInfo_InPut input) => service.Edit(input).ToJsonResult();
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">实体id</param>
        /// <returns></returns>
        [HttpPost, Route("del/{id}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Delete([FromRoute] int id) => service.Delete(id).ToJsonResult();
        /// <summary>
        /// 启用??
        /// </summary>
        /// <param name="id">ID</param> 
        /// <returns></returns>
        [HttpPost, Route("enable/{id}")]
        [ApiVersion("1.0")]
        [ApiVersion("2.0")]
        public IActionResult Enable(int id) => service.Enable(id).ToJsonResult();
    }
}
